package weiyao.xinxidasai.mapper;

import org.apache.ibatis.annotations.*;
import weiyao.xinxidasai.DTO.versionDTO;
import weiyao.xinxidasai.pojo.Version;

import java.util.List;

@Mapper
public interface VersionMapper {
    // 新增Version
    @Insert("INSERT INTO `version` (version_id, user_id, version_no, r, x0, iterations, create_time) " +
            "VALUES (#{versionId}, #{userId}, #{versionNo}, #{r}, #{x0}, #{iterations}, #{createTime})")
    @Options(useGeneratedKeys = true, keyProperty = "versionId")
    void insert(Version version);

    @Insert("INSERT INTO `version` (version_id,user_id, version_no, r, x0, iterations, create_time) " +
            "VALUES (#{versionId},#{userId}, #{versionNo}, #{r}, #{x0}, #{iterations}, #{createTime})")
    @Options(useGeneratedKeys = true, keyProperty = "versionId", keyColumn = "version_id")
    int insertandget(Version version);  // 返回影响的行数

    // 根据versionNo查询Version

    @Select("SELECT version_id, version_no, create_time FROM version WHERE version_no LIKE CONCAT('%', #{versionNo}, '%') AND user_id = #{userId}")
    List<versionDTO> getVersionByVersionNo(@Param("versionNo") String versionNo , @Param("userId") Long userId);

    // 根据userId查询所有Version
    @Select("select version_no from `version` where user_id=#{userId}")
    List<String> selectAllVersionIDByUserId(Long userId);

    // 根据versionId查询Version
    @Select("select * from `version` where version_id=#{versionId}")
    Version getVersionByVersionId(Long versionId);
    // 根据versionId查询Version
    @Select("select version_id, user_id, version_no, create_time from `version` where version_id=#{versionId}")
    versionDTO getVersionDTOByVersionId(Long versionId);
    // 根据userId查询所有Version
    @Select("select version_id, user_id, version_no, create_time from `version` where user_id=#{userId}")
    List<versionDTO> selectAllVersionsByUserId(Long userId);

    // 更新Version
    @Update("UPDATE `version` SET  version_no = #{versionNo}, create_time = now() WHERE version_id = #{versionId} AND user_id=#{userId}  ")
    int updateVersion(@Param("versionId") Long versionId,@Param("versionNo")String versionNo,@Param("userId")Long userId);

    // 删除Version
    @Delete("DELETE FROM `version` WHERE version_id=#{versionId}")
    int deleteVersionById(Long versionId);

    @Select("SELECT  version_no  FROM `version` ")
    List<String> getAllValidVersionNos();

}